﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HDT.GUI;
using DTO;
using BLL;

namespace HDT
{
    public partial class FrmDMucMH : Form
    {
        private string PRODUCT_IMAGE_FOLDER = "Image\\_Product\\";
        private List<LoaiMatHangDTO> dsLoai;
        private List<MatHangDTO> dsMH;

        public FrmDMucMH()
        {
            InitializeComponent();
            cboSXGia.SelectedIndex = 0;
            picMH.InitialImage = null;
        }

        public FrmDMucMH(TKDangNhapDTO taiKhoan)
            : this()
        {
            try
            {
                string MaLNV = new NhanVienBLL().GetByID(taiKhoan.MaNhanVien).MaLoaiNhanVien;
                PhanQuyenDTO phanQuyen = new PhanQuyenBLL().Select("MaLoaiNV = '" + MaLNV + "'")[0];
                PhanQuyen(phanQuyen);
            }
            catch
            {

            }
        }

        private void PhanQuyen(PhanQuyenDTO phanQuyen)
        {
            pnlLMHControl.Visible = phanQuyen.QLMatHang;
            pnlMHControl.Visible = phanQuyen.QLMatHang;
        }

        private void btnThemLoaiMH_Click(object sender, EventArgs e)
        {
            FrmThemLoaiMH frm_ThemLoaiMH = new FrmThemLoaiMH();
            frm_ThemLoaiMH.refreshForm = new FrmThemLoaiMH.MyDelegate(SearchLMH);
            frm_ThemLoaiMH.ShowDialog();
        }

        private void btnSuaLoaiMH_Click(object sender, EventArgs e)
        {
            FrmSuaLoaiMH frm_SuaLoaiMH = new FrmSuaLoaiMH(dgvLoaiMH.SelectedRows[0].Cells[0].Value.ToString());
            frm_SuaLoaiMH.refreshForm = new FrmSuaLoaiMH.MyDelegate(SearchLMH);
            frm_SuaLoaiMH.ShowDialog();
        }

        private void btnThemCK_Click(object sender, EventArgs e)
        {
            FrmThemMH frm_ThemCK = new FrmThemMH();
            frm_ThemCK.refreshForm = new FrmThemMH.MyDelegate(SearchMH);
            frm_ThemCK.ShowDialog();
        }

        private void btnSuaCK_Click(object sender, EventArgs e)
        {
            FrmSuaMH frm_SuaCK = new FrmSuaMH(dgvMH.SelectedRows[0].Cells[0].Value.ToString());
            frm_SuaCK.refreshForm = new FrmSuaMH.MyDelegate(SearchMH);
            frm_SuaCK.ShowDialog();
        }

        private void LoadLoaiMatHangData()
        {
            dgvLoaiMH.Rows.Clear();
            foreach (LoaiMatHangDTO loaiMH in dsLoai)
            {
                DataGridViewRow row = (DataGridViewRow) dgvLoaiMH.Rows[0].Clone();
                row.Cells[0].Value = loaiMH.MaLoaiMatHang;
                row.Cells[1].Value = loaiMH.TenLoaiMatHang;
                row.Cells[2].Value = loaiMH.TrangThai ? 1 : 0;
                dgvLoaiMH.Rows.Add(row);
            }
        }

        private void LoadMatHangData(List<MatHangDTO> dsMatHang)
        {
            dgvMH.Rows.Clear();
            foreach (MatHangDTO matHang in dsMatHang)
            {
                DataGridViewRow row = (DataGridViewRow)dgvMH.Rows[0].Clone();
                row.Cells[0].Value = matHang.MaMatHang;
                row.Cells[1].Value = matHang.TenMatHang;
                row.Cells[2].Value = matHang.DonViTinh;
                row.Cells[3].Value = matHang.TrangThai ? 1 : 0;
                dgvMH.Rows.Add(row);
            }
        }

        private void dgvLoaiMH_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvLoaiMH.SelectedRows.Count > 0)
            {
                SearchMH();
            }
            else
            {

            }
        }

        private void SearchLMH()
        {
            dsLoai = new LoaiMatHangBLL().Select("TenLoai LIKE '%" + txtLoaiMH.Text.Trim() + "%'");
            LoadLoaiMatHangData();
        }

        private void SearchMH()
        {
            try
            {
                string MaLMH = dgvLoaiMH.SelectedRows[0].Cells[0].Value.ToString();
                dsMH = new MatHangBLL().Select("LoaiMatHang = '" + MaLMH + "' AND TenMatHang LIKE '%" + txtTenMH.Text.Trim() + "%'", "TenMatHang " + (cboSXGia.SelectedIndex == 1 ? "DESC" : "ASC"));
                LoadMatHangData(dsMH);
                btnSuaLoaiMH.Enabled = true;
            }
            catch
            {
                dgvMH.Rows.Clear();
                btnSuaLoaiMH.Enabled = false;
            }
        }

        private void FrmDMucMH_Load(object sender, EventArgs e)
        {
            SearchLMH();
        }

        private void txtLoaiMH_TextChanged(object sender, EventArgs e)
        {
            SearchLMH();
        }

        private void txtTenMH_TextChanged(object sender, EventArgs e)
        {
            SearchMH();
        }

        private void cboSXGia_SelectedIndexChanged(object sender, EventArgs e)
        {
            SearchMH();
        }

        private void LoadChietKhauData(List<ChietKhauDTO> dsCK)
        {
            dgvCK.Rows.Clear();
            foreach (ChietKhauDTO CK in dsCK)
            {
                DataGridViewRow row = (DataGridViewRow)dgvCK.Rows[0].Clone();
                row.Cells[0].Value = CK.SoLuongToiThieu;
                row.Cells[1].Value = CK.TyLeChietKhau.ToString("#,##0.00 %");
                dgvCK.Rows.Add(row);
            }
        }

        private void dgvMH_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvLoaiMH.SelectedRows.Count > 0)
            {
                try
                {
                    string MaMH = dgvMH.SelectedRows[0].Cells[0].Value.ToString();                    
                    btnSuaMH.Enabled = true;
                    foreach (MatHangDTO matHang in dsMH)
                    {
                        if (matHang.MaMatHang == MaMH)
                        {
                            LoadMatHangInfo(matHang);
                            break;
                        }
                    }

                }
                catch
                {
                    lblTenMH.Text = "## Unknown ##";
                    lbGia.Text = "";
                    txtChiTietSP.Text = "";
                    dgvCK.Rows.Clear();
                    LoadProductImage(null);
                    btnSuaMH.Enabled = false;
                }
            }
            else
            {
                dgvMH.Rows.Clear();
            }
        }

        private void LoadMatHangInfo(MatHangDTO matHang)
        {
            lblTenMH.Text = matHang.TenMatHang;
            lbGia.Text = "Giá: " + matHang.Gia.Gia.ToString("#,##0.00 VNĐ");
            txtChiTietSP.Text = matHang.ChiTiet;
            LoadChietKhauData(matHang.DSChietKhau);
            LoadProductImage(matHang.HinhAnh);
        }

        private void LoadProductImage(string filename)
        {
            try
            {
                string path = PRODUCT_IMAGE_FOLDER + filename;
                Image img = Image.FromFile(path);
                picMH.Image = img;
            }
            catch (Exception)
            {
                picMH.Image = null;
            }
        }
    }
}
